package com.didi.gulimall.product.web;

import com.alibaba.fastjson.JSON;
import com.didi.gulimall.product.service.SkuInfoService;
import com.didi.gulimall.product.vo.SkuItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.ExecutionException;

/**
 * @author 10626
 */
@Controller
@Slf4j
@Api("详情页")
public class ItemController {
    @Autowired
    private SkuInfoService skuInfoService;

    @Value("${gulimall.home}")
    private String homeHtml;

//    @GetMapping("{id}.html")
    @ApiOperation("获取当前商品信息")
    public String getItem(
            @ApiParam(name = "id", value = "skuId", required = true)
            @PathVariable("id") Long skuId,
            Model mode) throws ExecutionException, InterruptedException {
        log.info("当前商品skuId为" + skuId);

        SkuItemVo skuItemVo = skuInfoService.getItem(skuId);
        if (skuItemVo == null) {
            return "redirect:" + homeHtml;
        }
        mode.addAttribute("item", skuItemVo);
        log.info("返回数据：{}", JSON.toJSONString(skuItemVo));
        return "item";
    }
}
